package net.wzw.controllar;

import net.wzw.rule.RainRule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import net.wzw.enums.BizCodes;
import net.wzw.exception.BizException;
import net.wzw.utils.ApiResult;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author wzw
 * @date 2025/4/2
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/api/alert/v1/")
@Api(tags = "告警模块")
@ApiOperation(value = "消息告警控制器")
public class AlertController {

//    @Autowired
//    private MailServiceImpl mailService;

    private static final String to = "13330987534@163.com";

    @GetMapping("/humidity")
    public ApiResult alertHumiday(@RequestParam(name = "humidity") int humidity) {

        Facts facts = new Facts();

        facts.put("humidity", humidity);

        RulesEngine rulesEngine = new DefaultRulesEngine();

        log.info("facts={}", facts);

        Map<Rule, Boolean> relueResultMap = rulesEngine.check(new Rules(new RainRule()), facts);

        if (CollectionUtils.isEmpty(relueResultMap)) {

            throw new BizException(BizCodes.COMMON_ALERT_FAIL);

        }

        //mailService.sendMail(to, "下雨湿度告警", "尊敬的用户您好，当前湿度大于80℃,请注意防潮!");

        rulesEngine.fire(new Rules(new RainRule()), facts);

        return ApiResult.doSuccess();

    }


}
